package cn.com.ddapp.ddm.bean.mine;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ypy.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.BankFragmentAdapter;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.fragment.base.mine.BindAlipayFragment;
import cn.com.ddapp.ddm.fragment.base.mine.BindBankFragment;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;


public class BindSettingActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.aliPay)
    RadioButton aliPayRb;
    @BindView(R.id.bank)
    RadioButton bankRb;
    @BindView(R.id.rg_bind)
    RadioGroup rgBind;
    /*@BindView(R.id.viewPager)
    ViewPager viewPager;*/


    private FragmentManager fragmentManager;
    private BindBankFragment bankFragment;
    private BindAlipayFragment alipayFragment;
    List<Fragment> fragments;
    public static BindSettingActivity instance;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
    }

    @Override
    public void initView() {
        titletext.setText("绑定设置");
        fragmentManager = getSupportFragmentManager();
        rgBind.check(R.id.aliPay);
        alipayFragment = (BindAlipayFragment) fragmentManager.findFragmentByTag("0");
        bankFragment = (BindBankFragment) fragmentManager.findFragmentByTag("1");
        showFragment(1);

        rgBind.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                MyBankInfo bundle ;
                switch (checkedId) {
                    case R.id.aliPay:
                        showFragment(1);
                        bundle = new MyBankInfo();
                        bundle.setKey("alipay");
                        EventBus.getDefault().post(bundle);
                        break;
                    case R.id.bank:
                        showFragment(2);
                        bundle = new MyBankInfo();
                        bundle.setKey("bank");
                        EventBus.getDefault().post(bundle);
                        break;
                }
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    private void showFragment(int index) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (bankFragment != null) {
            transaction.hide(bankFragment);
        }
        if (alipayFragment != null) {
            transaction.hide(alipayFragment);
        }
        if (index == 1) {
            if (alipayFragment == null) {
                alipayFragment = new BindAlipayFragment();
                transaction.add(R.id.linearL, alipayFragment,"0");
            } else {
                transaction.show(alipayFragment);
            }
        } else if (index == 2) {
            if (bankFragment == null) {
                bankFragment = new BindBankFragment();
                transaction.add(R.id.linearL, bankFragment,"1");
            } else {
                transaction.show(bankFragment);
            }
        }
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void initData() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_bind_bank_activityi;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

    /*@OnClick({R.id.aliPay,R.id.bank})
    public void onclick(View view){
        switch (view.getId()) {
            case R.id.aliPay:
//                rgBind.check(R.id.aliPay);
                showFragment(1);
                break;
            case R.id.bank:
//                rgBind.check(R.id.bank);
                showFragment(2);
                break;
        }
    }*/
}